package myJs.api

import myJs.myPkg.MyFetch
import myJs.tool.Tool
import org.scalajs.dom.FormData
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo.ValidData
import slinky.web.html.s
import upickle.default._

/**
 * Created by yz on 19/5/2022
 */
object WechatMemberApi {

  import shared.pojo.Pojo.wechatMember._

  val adminBaseUrl = s"${Api.apiUrl}/panda/admin/wechatMember"
  val baseUrl = s"${Api.apiUrl}/panda/wechatMember"

  def all = {
    val url = s"${Api.apiUrl}/panda/admin/wechatMember/all"
    MyFetch.getRnStr(url = s"${url}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[List[ujson.Obj]](str)
    }
  }

  def delete(id: String) = {
    val url = s"${Api.apiUrl}/panda/admin/wechatMember/delete"
    MyFetch.deleteRnStr(url = s"${url}?phone=${id}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def add(request: Add.Request) = {
    val url = s"${adminBaseUrl}/add"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).
      map { str =>
        str
      }
  }

  def update(request: WechatMemberData) = {
    val url = s"${baseUrl}/update"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def getByPhone(phone: String) = {
    val url = s"${baseUrl}/getByPhone"
    MyFetch.getRnStr(url = s"${url}?phone=${phone}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[WechatMemberData](str)
    }
  }

  def phoneCheck(request: PhoneData) = {
    val url = s"${adminBaseUrl}/phoneCheck"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json")).map { str =>
      read[ValidData](str)
    }
  }

}
